package cn.iocoder.boot.controller.admin;

import cn.hutool.json.JSONUtil;
import cn.iocoder.boot.dto.KbPlatformApiDTO;
import cn.iocoder.boot.entity.KbPlatformApi;
import cn.iocoder.boot.service.CallLimitService;
import cn.iocoder.boot.service.KbPlatformApiService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.config.SecurityProperties;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi;
import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.*;


@Tag(name = "平台API管理")
@RestController
@RequestMapping("/platformApi")
@Validated
public class PlatformApiController {

    @Resource
    private SecurityProperties securityProperties;
    @Resource
    private OAuth2TokenApi oauth2TokenApi;
    @Resource
    private KbPlatformApiService kbPlatformApiService;
    @Resource
    private CallLimitService callLimitService;

    @GetMapping("/get")
    @Operation(summary = "根据token获取用户信息")
    public CommonResult<String> get(HttpServletRequest request) {
        String token = SecurityFrameworkUtils.obtainAuthorization(request,
                securityProperties.getTokenHeader(), securityProperties.getTokenParameter());

        OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token);
        //获取用户信息
        LoginUser loginUser = getLoginUser();
        JSONUtil.toJsonStr(loginUser.getInfo());
        if (!callLimitService.canCall(accessToken.getUserId())) {
            return error(500,"您已超出每日调用限制，请明天再试！");
        }
        //业务逻辑

        return success(JSONUtil.toJsonStr(accessToken));
    }

    @PostMapping("/addOrUpdate")
    @Operation(summary = "新增or更新")
    public CommonResult<Boolean> add(@Valid @RequestBody KbPlatformApiDTO platformApiDTO) {
        return success((kbPlatformApiService.addOrUpdate(platformApiDTO)));
    }

    @PostMapping("/pageList")
    @Operation(summary = "分页查询")
    public CommonResult<Page<KbPlatformApiDTO>> pageList(@RequestBody KbPlatformApiDTO platformApiDTO) {
        return success((kbPlatformApiService.getPlatformApiPage(platformApiDTO)));
    }
    @GetMapping("/del/{id}")
    @Operation(summary = "删除")
    public CommonResult<Boolean> del(@PathVariable Long id ) {
        return success((kbPlatformApiService.del(id)));
    }
    @GetMapping("/getPlatformApi")
    @Operation(summary = "获取数据")
    public CommonResult<KbPlatformApi> getPlatformApi(Long id) {
        return success((kbPlatformApiService.getPlatformApiById(id)));
    }
}